extends Control
class_name SimpleHealthBar

# 简化的像素艺术血条组件
# 使用health_ui.png资源
# 这是基础血条类，PlayerHealthBar和EnemyHealthBar都继承自此类

@export var max_health: int = 100
@export var current_health: int = 100

# 节点引用
var background_sprite: Sprite2D
var health_sprite: Sprite2D

# 血条配置
var bar_width: int = 12
var bar_height: int = 2

func _ready():
	# 延迟一帧调用setup_health_bar，确保场景完全加载
	call_deferred("setup_health_bar")

func setup_health_bar():
	# 不再修改Control节点的size，而是通过缩放精灵来控制血条尺寸
	
	# 获取原始贴图尺寸
	var texture = load("res://Art/v1.1 dungeon crawler 16X16 pixel pack/ui (new)/health_ui.png")
	var texture_size = texture.get_size()
	
	# 计算缩放比例
	var scale_x = float(bar_width) / texture_size.x
	var scale_y = float(bar_height) / texture_size.y
	
	# 创建背景精灵
	background_sprite = Sprite2D.new()
	add_child(background_sprite)
	background_sprite.texture = texture
	background_sprite.position = Vector2(0, 0)  # 相对于Control节点的位置
	background_sprite.scale = Vector2(scale_x, scale_y)
	background_sprite.modulate = Color(0.3, 0.3, 0.3, 0.8)
	background_sprite.z_index = 0
	
	# 创建血量精灵
	health_sprite = Sprite2D.new()
	add_child(health_sprite)
	health_sprite.texture = texture
	health_sprite.position = Vector2(0, 0)  # 相对于Control节点的位置
	health_sprite.scale = Vector2(scale_x, scale_y)
	health_sprite.modulate = get_health_color()
	health_sprite.z_index = 1
	
	# 使用region来控制血条长度
	health_sprite.region_enabled = true
	
	update_health_display()
	
	# 添加调试信息
	print("血条设置完成 - 目标尺寸: ", bar_width, "x", bar_height, " 缩放比例: ", scale_x, "x", scale_y)

func set_health(current: int, maximum: int):
	current_health = clamp(current, 0, maximum)
	max_health = maximum
	update_health_display()

func update_health_display():
	if not health_sprite:
		return
	
	# 计算血量百分比
	var health_ratio = float(current_health) / float(max_health) if max_health > 0 else 0.0
	
	# 更新血条的region来显示相应的血量
	var texture_size = health_sprite.texture.get_size()
	var fill_width = int(texture_size.x * health_ratio)
	
	health_sprite.region_rect = Rect2(0, 0, fill_width, texture_size.y)
	
	# 调整position以保持左对齐（考虑缩放）
	var scale_x = health_sprite.scale.x
	var offset_x = -(texture_size.x * scale_x) / 2 + (fill_width * scale_x) / 2
	health_sprite.position = Vector2(offset_x, 0)
	
	# 根据血量调整颜色
	health_sprite.modulate = get_health_color()
	
	# 如果血量为0，隐藏血条
	visible = current_health > 0

func get_health_color() -> Color:
	var health_ratio = float(current_health) / float(max_health) if max_health > 0 else 0.0
	
	if health_ratio > 0.6:
		return Color.GREEN  # 健康状态
	elif health_ratio > 0.3:
		return Color.YELLOW  # 警告状态
	else:
		return Color.RED  # 危险状态

# 显示血条（带淡入效果）
func show_health_bar():
	visible = true
	modulate.a = 0.0
	
	var tween = create_tween()
	tween.tween_property(self, "modulate:a", 1.0, 0.2)

# 隐藏血条（带淡出效果）
func hide_health_bar(delay: float = 3.0):
	var tween = create_tween()
	tween.tween_interval(delay)
	tween.tween_property(self, "modulate:a", 0.0, 0.3)
	tween.tween_callback(func(): visible = false)

# 血条动画效果
func animate_damage():
	if not health_sprite:
		return
	
	var tween = create_tween()
	tween.tween_property(health_sprite, "modulate", Color.WHITE, 0.1)
	tween.tween_property(health_sprite, "modulate", get_health_color(), 0.1)
